<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8" contentType="text/html; charset=utf-8" %>
<%@include file="/quick/include/common.jsp"%>
<div id='productTable_win' style='display:none'></div>
<div id='productTable_html' style='display:none'></div>
<script>
function f_addProduct(_afterSelected,_beforeSelect){
  Ext.get('productTable_win').dom.innerHTML="<div id='tree_div' style='display:block'></div>"; 
  var tree = new Ext.tree.TreePanel({
        el:'tree_div',
	    region:'west',
	    width:200,
	    loader: new Ext.tree.TreeLoader({dataUrl: g_rootPath+'product/getProductLevelTree.html'})
	});
  var root = new Ext.tree.AsyncTreeNode({id:'0',text:'产品等级',productId:'0',icon:'../../../extjs/resources/images/default/grid/hmenu-lock.png'});
  tree.setRootNode(root);
  
  var htmlDiv=Ext.get("productTable_html").dom;
  var noteProductTable="V_NOTE_PRODUCT";
  var productTable='T_PRODUCT';
  var params={tableName:productTable};  
  if(typeof(_beforeSelect)=='function'){
    _beforeSelect(params);
  }	  
  tree.on('click',function(node){
    params=Ext.apply(params,{REFID:node.attributes.productId});
    globalScope._model(params,function(responseText){
	   htmlDiv.innerHTML=responseText;
	   var span=Ext.get(productTable+"_columns").dom;
	   var columns=Ext.decode(span.innerText);
	   var productGrid= new Ext.quick.QuickGrid(productTable, {
	     id:'c_grid',
	     collapsible:true,
	     split:true,
	     stripeRows: true, 
	     enableDragDrop:false,
	     title:'产品列表',
	     clicksToEdit:1,
	     'remove':true,
	     height:g_height*85,
	     'smConfig':{}
	   },columns);
	   
	   productGrid.on('afteredit',function(e){
	     var r=e.record;
	     var v1=parseFloat(e.value);
	     var v2=parseFloat(r.get('STOCK_NUM'));
	     if(v1>v2){
	       Ext.Msg.alert('<fmt:message key="label.warn"/>','出库数量('+v1+')不能大于库存数量('+v2+')');
	       r.reject();
	     }
	   });	   
	   
	   globalScope.grid.add(productTable,productGrid);
	   
	   var p=Ext.getCmp('c_product');
	   p.remove('c_grid');
	   p.add(productGrid);
	   p.doLayout();
   });     
  });
  

  var selectProductWin= new Ext.Window({
    id:'c_win2',
    el:'productTable_win',
    layout:'border',
       title:'选择产品',
    width:1000,
    height:g_height*1.2,
    closeAction:'hide',
    buttonAlign:'center',
    items: [tree,{id:'c_product',region:'center',layout:'fit',items:[{id:'c_grid'}] }],
    buttons: [{
	        text:'<fmt:message key="button.conf"/>',
	        handler:function(){
                    var m=globalScope.grid.get(productTable).getSelectionModel().getSelections(); 
				    if(m.length==0){
				      Ext.Msg.confirm('title','没有选择数据,确认吗',function(btn){
                        if(btn=='yes'){
                          commonQueryWin.hide();
                        }
                      });
                      return;
				    }
				    if(typeof(_afterSelected)=='function'){
				      _afterSelected(m);
				    }

			    selectProductWin.hide();
            }
	      },{
	        text:'<fmt:message key="button.close"/>',
	        handler:function(){selectProductWin.hide();}
	     }]
	});  
   selectProductWin.render(document.body);  
   selectProductWin.show(); 	 

}

</script>
